/**
 * 
 */
package com.hw.hcit.feature;

import java.util.Random;

import com.hw.hcit.R;
import com.hw.hcit.conf.Config;
import com.hw.hcit.conf.JCtrlCallback;
import com.hw.hcit.conf.JTestItem;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;


/**
 * @author gaoshengpan
 *
 */
public class ReceiverTestItem extends JTestItem {
   private TextView textView,textView2,textView3;
   private MediaPlayer mMediaPlayer;
   private AudioManager mAudioManager;
   private SoundPool mSoundPool;
   private int soundID;
   private boolean tested = false;
   private boolean powerClicked = false;
   private String TAG = "ReceiverTestItem";
   private Button mButton1,mButton2;
   private RadioButton bt1,bt2,bt3;
   private RadioGroup group;
   private static int sourceId;
   private static int chooseTime = 0;
   private Handler mHandler =new Handler(){
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			tested = true;
		}
	};

   /**
    * @param cb
    */
   public ReceiverTestItem(JCtrlCallback cb) {
      super(cb);
      // TODO Auto-generated constructor stub
   }

   /**
    * 
    */
   public ReceiverTestItem() {
      // TODO Auto-generated constructor stub
   }

   @Override
   public void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
      mAudioManager = (AudioManager) Config.GetActivity()
            .getApplicationContext().getSystemService("audio");
      mAudioManager.setMode(AudioManager.MODE_IN_CALL);
      mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
            mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
   }

   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
         Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      View rootView = inflater.inflate(R.layout.receiver, container,
            false);
      textView = (TextView) rootView.findViewById(R.id.textView1);
      /*15-09-28 gaoshenpan fix title string begin */
     /* textView2 = (TextView) rootView.findViewById(R.id.test_title);
      textView2.setText(getString(R.string.speaker_test));*/
      /*15-09-28 gaoshenpan fix title string end */
      textView3 = (TextView) rootView.findViewById(R.id.textView2);
      textView.setText(getString(R.string.receiver_test));
      bt1 = (RadioButton)rootView.findViewById(R.id.radio0);
      bt2 = (RadioButton)rootView.findViewById(R.id.radio1);
      /* 15-09-17 gaoshenpan fix remove test2.mp3 begin*/
      /*bt3 = (RadioButton)rootView.findViewById(R.id.radio2);*/
      group = (RadioGroup)rootView.findViewById(R.id.radioGroup1);
      group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup arg0, int checkedId) {
				// TODO Auto-generated method stub
				chooseTime++;
				switch(checkedId){
				/*case R.id.radio2:
					if(chooseTime<=1){
					if(sourceId == R.raw.test2 ){
						 mButton1.setEnabled(true);
						 textView3.setText(getString(R.string.right_choice));
					}else{
						textView3.setText(getString(R.string.wrong_choice));
					}
					}
					textView3.setVisibility(View.VISIBLE);
					break;*/
				 /* 15-09-17 gaoshenpan fix remove test2.mp3 end*/
				case R.id.radio0:
					if(chooseTime<=1){
					if(sourceId == R.raw.testl ){
						 mButton1.setEnabled(true);
						 textView3.setText(getString(R.string.right_choice));
					}else{
						textView3.setText(getString(R.string.wrong_choice));
					}
					}
					textView3.setVisibility(View.VISIBLE);
					break;
				case R.id.radio1:
					if(chooseTime<=1){
					if(sourceId == R.raw.testr ){
						 mButton1.setEnabled(true);
						 textView3.setText(getString(R.string.right_choice));
					}else{
						textView3.setText(getString(R.string.wrong_choice));
					}
					}
					textView3.setVisibility(View.VISIBLE);
					break;
				}
			}
		});
      mButton1 = (Button) rootView.findViewById(R.id.button1);
		mButton2 = (Button) rootView.findViewById(R.id.button2);
		mButton1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(Config.CURR_PROJECT.isAuto()){
					if (!Config.ageingTested) {
						rewriteOneByteOfNv2499(15, 'P');
					} else {
						rewriteOneByteOfNv2499(46, 'P');
					}
				}
				 testFinish(true, 1, true);
				
			}
		});
		mButton2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(Config.CURR_PROJECT.isAuto()){
					if (!Config.ageingTested) {
						rewriteOneByteOfNv2499(15, 'F');
					} else {
						rewriteOneByteOfNv2499(46, 'F');
					}
				}
				testFinish(false, 1, false);
				
			}
		});
      return rootView;
   }

   @Override
   public void onResume() {
      // TODO Auto-generated method stub
      super.onResume();
      /* 15-09-17 gaoshenpan fix remove test2.mp3 begin*/
      mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 5);
      tested = false;
      powerClicked = false;
      mButton1.setEnabled(false);
      chooseTime = 0;
      textView3.setVisibility(View.GONE);
      bt1.setChecked(false);
      bt2.setChecked(false);
      /*bt3.setChecked(false);*/
      if (mSoundPool != null) {
         Log.d(TAG, "receiver---------->onpause");
         Random random = new Random(); 
         int i =(int) random.nextInt(2);
         Log.d(TAG, "id-------->"+"="+i+"\n"+random.nextInt(3));
			switch (i) {
			case 0:
				sourceId = R.raw.testl;
				break;
			case 1:
				sourceId = R.raw.testr;
				break;
			
			default:
				break;
			}
		/* 15-09-17 gaoshenpan fix remove test2.mp3 end*/
         soundID = mSoundPool.load(Config.GetActivity()
               .getApplicationContext(), sourceId, 1);
         mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {

            @Override
            public void onLoadComplete(SoundPool arg0, int arg1, int arg2) {
               // TODO Auto-generated method stub
               mSoundPool.play(soundID, 1, 1, 1, -1, 1);
               mHandler.sendEmptyMessageDelayed(0, 1500);
               
            }
         });
         Log.d(TAG, "receiver---------->soundID" + soundID);

      }
   }

   @Override
   public void onPause() {
      // TODO Auto-generated method stub
      Log.d(TAG, "receiver---------->onpause");
      super.onPause();
      bt1.setChecked(false);
      bt2.setChecked(false);
   }

   @Override
   public void onStop() {
      // TODO Auto-generated method stub
      Log.d(TAG, "receiver---------->stop");
      super.onStop();
      /* 15-09-17 gaoshenpan fix onpause release source begin*/
      if (mSoundPool !=null) {
    	  mSoundPool.pause(0);
          mSoundPool.stop(0);
          mSoundPool.release();
       }
      /* 15-09-17 gaoshenpan fix onpause release source end*/
      
   }

   @Override
   public void onDestroy() {
      // TODO Auto-generated method stub

      super.onDestroy();
      if (mAudioManager != null) {
         mAudioManager.setMode(AudioManager.MODE_NORMAL);
      }
      if (mSoundPool != null) {
         mSoundPool.stop(0);
         mSoundPool.release();
      }
   }
   @Override
   public boolean dispatchKeyEvent(KeyEvent event) {
       // TODO Auto-generated method stub
       return true;
   }
   /*@Override
   public boolean dispatchKeyEvent(KeyEvent event) {
       // TODO Auto-generated method stub
       if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN) {
           dissmissPowerClicked();
           Log.d(TAG, " --------->KeyEvent.KEYCODE_VOLUME_DOWN");
           if (powerClicked) {
               Log.d(TAG,
                       "powerClicked --------->KeyEvent.KEYCODE_VOLUME_DOWN");
               testFinish(false, 1, false);
               return true;
           }
           if (tested) {
               Log.d(TAG,
                       "tested --------->KeyEvent.KEYCODE_VOLUME_DOWN");
               testFinish(true, 1, true);
           } else {
               Log.d(TAG, " --------->showTestUncomplete");
               showTestUncomplete();
           }
       } else if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP) {
           dissmissPowerClicked();
           testFinish(true, -1, true);
       } else if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
           Log.d(TAG, "powerClicked --------->KeyEvent.KEYCODE_POWER");
           powerClicked = true;
           showPowerClicked();
       }
       return true;
   }*/


}
